package com.mopon.springboot.jdbc.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ProductDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public void addProduct(String name) {
		String sql = "insert into product(name) values('" + name + "')";
		jdbcTemplate.execute(sql);
	}
	
	/**
	 * rollbackFor 设置对哪些异常进行回滚，默认是运行时异常
	 * @param names
	 */
	@Transactional(rollbackFor=Exception.class)
	public void addProductBatch(String... names) {
		for (String name : names) {
			String sql = "insert into product(name) values('" + name + "')";
			jdbcTemplate.execute(sql);
		}
	}
}
